﻿// Copyright © 2011 Paul Vanukoff (pvanukoff@gmail.com)

namespace Bravura.Native.PluginBase.Oscillators
{
	/// <summary>
	/// Base class for oscillators.
	/// </summary>
	public abstract class Oscillator : ISampleable
	{
		protected double _dt = 1.0 / 48000.0;

		// base values
		protected double _ampBase = 1.0;
		protected double _distBase = 1.0;
		protected double _freqBase = 100.0;

		// modulators
		protected double _ampMod = 1.0;
		protected double _distMod = 0.0;
		protected double _freqMod = 1.0;

		// actual values
		protected double _amplitude = 1.0;
		protected double _distortion = 1.0;
		protected bool _applyDist = false;

		/// <summary>
		/// Constructor
		/// </summary>
		protected Oscillator(double dt)
		{
			_dt = dt;
		}

		/// <summary>
		/// Sets the amplitude.
		/// </summary>
		public void SetAmplitude(double val)
		{
			_ampBase = val;
			_amplitude = _ampBase * _ampMod;
		}

		/// <summary>
		/// Sets the distortion.
		/// </summary>
		public void SetDistortion(double val)
		{
			_distBase = val;
			_distortion = _distBase + _distMod;
			_applyDist = true;
		}

		/// <summary>
		/// Sets the frequency.
		/// </summary>
		public abstract void SetFrequency(double val);

		/// <summary>
		/// Modulates the amplitude.
		/// </summary>
		public void ModulateAmplitude(double val)
		{
			_ampMod = val;
			_amplitude = _ampBase * _ampMod;
		}

		/// <summary>
		/// Modulates the distortion.
		/// </summary>
		public void ModulateDistortion(double val)
		{
			_distMod = val;
			_distortion = _distBase + _distMod;
			_applyDist = true;
		}

		/// <summary>
		/// Modulates the frequency.
		/// </summary>
		public abstract void ModulateFrequency(double val);

		/// <summary>
		/// Gets the next sample.
		/// </summary>
		public abstract double Sample();
	}
}
